package com.zcl.demo7.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.zcl.demo7.DpUtils;
import com.zcl.demo7.R;
import com.zcl.demo7.bean.HomeBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RlvAdapter extends RecyclerView.Adapter<RlvAdapter.ViewHolder> {
    private List<HomeBean.ResultsBean> beans = new ArrayList<>();
    private Context context;
    public boolean b=true;

    public RlvAdapter(Context context) {
        this.context = context;
    }

    public void getData(List<HomeBean.ResultsBean> beans) {
        this.beans.addAll(beans);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View inflate = LayoutInflater.from(context).inflate(R.layout.item, viewGroup, false);

        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        HomeBean.ResultsBean resultsBean = beans.get(i);
        ViewGroup.LayoutParams layoutParams = viewHolder.itemView.getLayoutParams();

        if(b){
            float v = DpUtils.dpToPx(this.context, 400);
            layoutParams.height= (int) v;
        }else{
            int a=new Random().nextInt(800-300+1)+300;
            float v = DpUtils.dpToPx(this.context, a);

            layoutParams.height= (int) v;
        }
        Glide.with(context).load(resultsBean.getUrl()).into(viewHolder.mIvImg);
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView mIvImg;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mIvImg = (ImageView) itemView.findViewById(R.id.iv_img);
        }
    }



}
